# coding=utf-8
# 读取lib库
import os, re

from test.file.util import listDir

_root_path = "/Users/wangjian/Downloads/tom/src/common/"


def main(_file_name, _libs, _is_container):
    _files = []
    # 找到目录下有多少个文件
    listDir(_file_name, '%s%s' % (_root_path, _file_name), _files)
    # 遍历找到的文件列表
    for file in _files:
        name = file['name']
        path = file['path']
        if name == 'index.js' or name == 'index.jsx' or str(name).__contains__('_bak'):
            continue
        getFileContant(name, path, _libs, _is_container)
    pass


def getFileContant(name, path, _libs, _is_container):
    file_object = open(path, 'r')
    all_the_text = file_object.read()
    # 组件名取文件名
    lib_name = str(path).replace(_root_path, '').replace('/', '@@')
    _data = {}
    _data['lib_name'] = lib_name
    _data['lib_import_sentence'] = str(path).replace('/%s' % name, '').replace(_root_path, '')
    _data['lib_type'] = str(name).split('.')[0]
    _data['lib_is_container'] = _is_container
    # 组件参数
    if not re.search("([\\s\\S]*)(.propTypes)(\\s+)(=)(\\s+\{)([\\s\\S]+?)(\};)([\\s\\S]+)", all_the_text):
        _libs.append(_data)
        return
    attrs = re.sub("([\\s\\S]*)(.propTypes)(\\s+)(=)(\\s+\{)([\\s\\S]+?)(\};)([\\s\\S]+)", "\\6", all_the_text)
    _lib_attr = {}
    _lib_event = {}
    for _attr in str(attrs).replace(' ', '').replace('\n', '').split(','):
        if _attr == '':
            continue
        if str(_attr).__contains__('PropTypes.func'):
            _lib_event[str(_attr).split(':')[0]] = str(_attr).split(':')[1]
        else:
            _lib_attr[str(_attr).split(':')[0]] = str(_attr).split(':')[1]

    _data['attrs'] = _lib_attr
    _data['event'] = _lib_event
    _libs.append(_data)
    pass


if __name__ == '__main__':
    _libs = []
    _sys = "tom"
    main("components", _libs, '1')
    main("containers", _libs, '0')
    for _lib in _libs:
        print _lib
    pass
